<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
     <el-form-item label="学期" prop="semId">
       <el-select v-model="queryParams.semId" filterable clearable placeholder="请选择学期">
         <el-option
           v-for="item in semsterList"
           :key="item.id"
           :label="item.title"
           :value="item.id"
           size="small">
         </el-option>
       </el-select>
     </el-form-item>
    <el-form-item label="年级" prop="year">
        <el-select v-model="queryParams.year" clearable placeholder="请选择年级" >
            <el-option
            v-for="item in yearList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
            size="small">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="专业" prop="specId">
        <el-select v-model="queryParams.specId" clearable placeholder="请选择专业" >
          <el-option
            v-for="item in specialtyList"
            :key="item.id"
            :label="item.title"
            :value="item.id"
            size="small">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="班级" prop="classId">
        <el-select v-model="queryParams.classId" clearable placeholder="请选择班级">
          <el-option
            v-for="item in classList"
            :key="item.id"
            :label="item.title"
            :value="item.id"
            size="small">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="提交状态" prop="submits">
        <el-select v-model="queryParams.submits" placeholder="请选择确认状态" clearable size="small">
          <el-option
            v-for="dict in submitsOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="eduTaskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="学期" align="center" prop="semName" />
      <el-table-column label="年级" align="center" prop="year" />
      <el-table-column label="专业" align="center" prop="specName" />
      <el-table-column label="课程" align="center" prop="courseName" />
      <el-table-column label="班级" align="center" prop="className" />
      <el-table-column label="教学形式" align="center" prop="mode" :formatter="modeFormat" />
      <el-table-column label="考核类型" align="center" prop="exam" :formatter="examFormat" />
      <el-table-column label="教材" align="center" prop="bookName" />
      <el-table-column label="成绩提交状态" align="center" prop="submits" :formatter="submitsFormat"/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listEduTask, myEduTask, getEduTask, delEduTask, addEduTask, updateEduTask, exportEduTask } from "@/api/education/eduTask";
import { exportExamStudent, saveExamStudent, listExamStudent } from "@/api/exam/examResult";
import { getToken } from "@/utils/auth";
import { selectSpecialty} from "@/api/base/specialty";
import { selectSemster} from "@/api/base/semster";
import { selectCourse} from "@/api/base/course";

export default {
  name: "EduTask",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学期数据
      semsterList: [],
      //起始年
      yearList:[],
      // 专业表格数据
      specialtyList: [],
      // 课程数据
      courseList: [],
      // 班级数据
      classList: [],
      // 教学任务表格数据
      eduTaskList: [],
      //学生列表
      studentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 教学形式字典
      modeOptions: [],
      // 考核类型字典
      examOptions: [],
      // 提交状态类型字典
      submitsOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        semId: null,
        specId: null,
        courseId: null,
        mode: null,
        classId: null,
        teacherId: null,
        exam: null,
        result: null,
        bookId: null,
        roomId: null,
        score: null,
        periods: null,
        startWeek: null,
        endWeek: null,
        addTime: null,
        addUser: null,
        updateUser: null,
        status: 'Y'
      },
      resultSet:'',
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        year: [
          { required: true, message: "年级不能为空", trigger: "blur" }
        ],
        semId: [
          { required: true, message: "学期不能为空", trigger: "blur" }
        ],
        specId: [
          { required: true, message: "专业不能为空", trigger: "blur" }
        ],
        courseId: [
          { required: true, message: "课程不能为空", trigger: "blur" }
        ],
        classId: [
          { required: true, message: "班级不能为空", trigger: "blur" }
        ],
        teacherId: [
          { required: true, message: "教师不能为空", trigger: "blur" }
        ],
        bookId: [
          { required: true, message: "教材不能为空", trigger: "blur" }
        ],
        roomId: [
          { required: true, message: "教室不能为空", trigger: "blur" }
        ],
        periods: [
          { required: true, message: "学时不能为空", trigger: "blur" }
        ],
        startWeek: [
          { required: true, message: "开始周不能为空", trigger: "blur" }
        ],
        endWeek: [
          { required: true, message: "结束周不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("education_mode").then(response => {
      this.modeOptions = response.data;
    });
    this.getDicts("exam_type").then(response => {
      this.examOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.submitsOptions = response.data;
    });
    this.getConfigKey("sys.time.start").then(response => {
      var st = parseInt(response.msg);
      var dt = new Date();
      while(st<=dt.getFullYear()+1){
        this.yearList.push({value: st,label: st});
        st ++;
      }
    });
    this.getConfigKey("edu.result.set").then(response => {
      this.resultSet = response.msg;
    });
    selectSpecialty({'status':1}).then(response => {
      this.specialtyList = response.rows;
    });
    selectSemster({'status':1}).then(response => {
      this.semsterList = response.rows;
    });
    selectCourse().then(response => {
        this.courseList = response.rows;
        this.total = response.total;
      });
    selectClazz().then(response => {
        this.classList = response.rows;
        this.total = response.total;
      });
  },
  methods: {
    /** 查询教学任务列表 */
    getList() {
      this.loading = true;
      listEduTask(this.queryParams).then(response => {
        this.eduTaskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 教学形式字典翻译
    modeFormat(row, column) {
      return this.selectDictLabel(this.modeOptions, row.mode);
    },
    // 考核类型字典翻译
    examFormat(row, column) {
      return this.selectDictLabel(this.examOptions, row.exam);
    },
    // 确认状态字典翻译
    submitsFormat(row, column) {
      return this.selectDictLabel(this.submitsOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        semId: null,
        specId: null,
        courseId: null,
        mode: null,
        classId: null,
        teacherId: null,
        exam: "1",
        result: this.resultSet.replace(/\s/g,"\r\n"),
        bookId: null,
        roomId: null,
        score: null,
        periods: null,
        startWeek: null,
        endWeek: null,
        addTime: null,
        addUser: null,
        updateTime: null,
        updateUser: null,
        status: "N"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加教学任务";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getEduTask(row.id).then(response => {
        this.form = response.data;
        listExamStudent(row.id).then(response => {
          this.studentList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
        this.open = true;
        this.title = "在线录入成绩";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateEduTask(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEduTask(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 保存成绩 */
    saveResult(row) {
      var data = row;
      for(var i=0;i<this.studentList.length;i++){
        if(row.studentId == this.studentList[i]['studentId']){
          data = this.studentList[i];
        }
      }
      saveExamStudent(data).then(response => {
        this.msgSuccess("保存成功");
      });
    },
    /** 导出按钮操作 */
    handleStudent(row) {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有该教学任务下的学生名单吗?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportExamStudent(row.id);
        }).then(response => {
          this.download(response.msg);
        })
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "成绩导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      importTemplate().then(response => {
        this.download(response.msg);
      });
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    }
  }
};
</script>
